package service.Impl;

import com.alibaba.fastjson.JSONObject;
import entity.CurriculumList;
import lombok.extern.slf4j.Slf4j;
import mapper.CurriculumListDao;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import service.CurriculumListService;
import utils.ExcelImportUtils;
import utils.PageObject;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (CurriculumList)表服务实现类
 *
 * @author makejava
 * @since 2021-12-13 16:02:32
 */
@Service("curriculumListService")
@Slf4j
public class CurriculumListServiceImpl implements CurriculumListService {
    @Resource
    private CurriculumListDao curriculumListDao;

    @Override
    public List<CurriculumList> getTheacherAndCourse() {
        List<CurriculumList> curriculumLists = curriculumListDao.getTheacherAndCourse();
        return curriculumLists;
}

    /**
     * 通过ID查询单条数据
     *
     * @param pkId 主键
     * @return 实例对象
     */
    @Override
    public CurriculumList queryById(Integer pkId) {
        return this.curriculumListDao.queryById(pkId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CurriculumList> queryAllByLimit(int offset, int limit) {
        return this.curriculumListDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param insertobj 实例对象
     * @return 实例对象
     */
    @Override
    public JSONObject insert(JSONObject insertobj) {
        JSONObject resultobj = new JSONObject();
        CurriculumList curriculumList = new CurriculumList();
        curriculumList.setCurriculumid(insertobj.getString("curriculumid"));//课程编号
        curriculumList.setCurriculumname(insertobj.getString("curriculumname"));//课程名称
        curriculumList.setCurriculumteacher(insertobj.getString("curriculumteacher"));//任课老师
        curriculumList.setGrade(insertobj.getString("grade"));//年级
        curriculumList.setIsopen(insertobj.getString("isopen"));//是否开卷
        curriculumList.setPkId(insertobj.getInteger("pkId"));

        if (curriculumList.getPkId() == null || curriculumList.getPkId().equals(0)) {
            List<CurriculumList> curriculumLists = curriculumListDao.queryAll(curriculumList);
            if (curriculumLists.size() > 0) {
                resultobj.put("code", "-1");
                resultobj.put("msg", "已存在此数据,请核对!");
            } else {
                int insert = curriculumListDao.insert(curriculumList);
                if (insert == 1) {
                    resultobj.put("code", "0");
                    resultobj.put("msg", "数据新增成功!");
                } else {
                    resultobj.put("code", "-1");
                    resultobj.put("msg", "数据新增失败!");
                }
            }
            System.out.println(resultobj.toJSONString());
            return resultobj;
        } else {
            int update = curriculumListDao.update(curriculumList);
            if (update == 1) {
                resultobj.put("success", true);
                resultobj.put("msg", "修改数据成功");
            } else {
                resultobj.put("success", false);
                resultobj.put("msg", "修改数据失败");
            }
            return resultobj;
        }


    }

    /**
     * 修改数据
     *
     * @param updateobj 实例对象
     * @return 实例对象
     */
    @Override
    public JSONObject update(JSONObject updateobj) {
        JSONObject resultobj = new JSONObject();
        Integer pk_id = updateobj.getInteger("pk_Id");
        CurriculumList curriculumList = new CurriculumList();
        curriculumList.setPkId(pk_id);
        curriculumList.setIsopen(updateobj.getString("isopen"));
        curriculumList.setGrade(updateobj.getString("grade"));
        curriculumList.setCurriculumteacher(updateobj.getString("curriculumteacher"));
        curriculumList.setCurriculumname(updateobj.getString("curriculumname"));
        curriculumList.setCurriculumid(updateobj.getString("curriculumid"));
        int update = curriculumListDao.update(curriculumList);
        if (update == 1) {
            resultobj.put("success", true);
            resultobj.put("msg", "修改数据成功");
        } else {
            resultobj.put("success", false);
            resultobj.put("msg", "修改数据失败");
        }
        return resultobj;
    }

    /**
     * 通过主键删除数据
     *
     * @param pkId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer pkId) {
        return this.curriculumListDao.deleteById(pkId) > 0;
    }

    /**
     * 查询所以数据并返回
     *
     * @param jsonObject 查询条件
     * @return pageobject类型对象
     */
    @Override
    public PageObject selectAll(JSONObject jsonObject) {
        PageObject pageObject = new PageObject();
        //当前第几页
        Integer page = jsonObject.getInteger("page");
        //每页几条
        Integer limit = jsonObject.getInteger("limit");
        //计算开始位置
        Integer startindex = (page - 1) * limit;
        log.info("=====" + startindex);
        List<CurriculumList> curriculumLists = curriculumListDao.selectAll(startindex, limit);
        //查询所有数据条数
        Integer selectcount = curriculumListDao.selectcount();
        pageObject.setPageCount(page);//第几页
        pageObject.setPageSize(limit);//每页几条
        pageObject.setRowCount(selectcount);//总条数
        pageObject.setPageCount(selectcount / limit);//总页数
        pageObject.setRecords(Collections.singletonList(curriculumLists));//当前需展示数据
        return pageObject;
    }

    @Override
    public JSONObject excelimport(MultipartFile file) throws Exception {
        List<CurriculumList> list = new ArrayList<>();
        JSONObject result = new JSONObject();
        Workbook workbook = null;
        String fileName = file.getOriginalFilename();
        if (fileName.endsWith("xls")) {
            workbook = new HSSFWorkbook(file.getInputStream());
        } else if (fileName.endsWith("xlsx")) {
            workbook = new XSSFWorkbook(file.getInputStream());
        } else {
            throw new Exception("传输文件不是Excel文件!");
        }
        Sheet sheet = workbook.getSheet("Sheet1");
        int rows = sheet.getLastRowNum();
        if (rows == 0) {
            throw new Exception("行数不能为0");
        }
        int s = 0;
        for (int i = 1; i < rows + 1; i++) {
            //读取左上端单元格
            Row row = sheet.getRow(i);
            //行不为空
            if (row != null) {
                //读取cell
                CurriculumList curriculumList = new CurriculumList();
                //课程id
                String curriculumid = ExcelImportUtils.getCellValue(row.getCell(0));
                curriculumList.setCurriculumid(curriculumid);
                //课程名称
                String curriculumname = ExcelImportUtils.getCellValue(row.getCell(1));
                curriculumList.setCurriculumname(curriculumname);
                //是否开卷
                String isopen = ExcelImportUtils.getCellValue(row.getCell(2));
                curriculumList.setIsopen(isopen);
                //任课教师
                String curriculumteacher = ExcelImportUtils.getCellValue(row.getCell(3));
                curriculumList.setCurriculumteacher(curriculumteacher);
                //开课年级
                String grade = ExcelImportUtils.getCellValue(row.getCell(4));
                curriculumList.setGrade(grade);
                int insert = curriculumListDao.insert(curriculumList);
                s++;
                list.add(curriculumList);//把实数据放入集合里
            }
        }
        if (s == rows) {
            result.put("code", "0");
            result.put("msg", "数据导入成功!");
        } else {
            result.put("code", "-1");
            result.put("msg", "数据导入失败!");
        }
        return result;
    }
}
